package com.qf.face.utils;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.qf.base.utils.Base64Util;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author 30909
 * 集成百度的人脸识别
 */
public class BaiduUtil {



    /**
     * 对比两个人是否为同一个re人
     */
    public void sample(AipFace client) {


        String image1 = Base64Util.image2Base64("E:\\FACE\\111.png");
        String image2 = Base64Util.image2Base64("E:\\FACE\\dsh.jpg");

        // image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应。
        MatchRequest req1 = new MatchRequest(image1, "BASE64");
        MatchRequest req2 = new MatchRequest(image2, "BASE64");
        ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
        requests.add(req1);
        requests.add(req2);

        JSONObject res = client.match(requests);
        System.out.println(res.toString(2));

    }


    /**
     * 添加一个人脸
     */
    public static void addFace(AipFace client ,String image,Long userId) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("user_info", "user's info");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");
        options.put("action_type", "REPLACE");

        //String image = "取决于image_type参数，传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
        String imageType = "BASE64";
        String groupId = "dsh";
        //String userId = "user1";

        // 人脸注册
        JSONObject res = client.addUser(image, imageType, groupId, userId+"", options);
        System.out.println(res.toString(2));

    }
    /**
     * 修改
     */
    public static void updateFace(AipFace client ,String image,Long userId) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("user_info", "user's info");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");
        options.put("action_type", "REPLACE");

        //String image = "取决于image_type参数，传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
        String imageType = "BASE64";
        String groupId = "dsh";
        //String userId = "user1";

        // 人脸更新
        JSONObject res = client.updateUser(image, imageType, groupId, userId+"", options);
        System.out.println(res.toString(2));

    }
    /**
     * 删除
     */
    public  static  void delFace(AipFace client,Long userId) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();

    //    String userId = "user1";
        String groupId = "dsh";
        String faceToken = "face_token_23123";

        // 人脸删除
        JSONObject res = client.faceDelete(userId+"", groupId, faceToken, options);
        System.out.println(res.toString(2));

    }
    /**
     * 搜索一个人脸
     */
    public static Long search(AipFace client ,String image) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        //options.put("max_face_num", "3");
        options.put("match_threshold", "70");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");
        //options.put("max_user_num", "3");


        String imageType = "BASE64";
        String groupIdList = "dsh";

        // 人脸搜索
        JSONObject res = client.search(image, imageType, groupIdList, options);
        if(res.getInt("error_code")==0) {
            String userIdString = res.getJSONObject("result").getJSONArray("user_list").getJSONObject(0).getString("user_id");
            return Long.parseLong(userIdString);
        }else{
            return null;
        }
    }
}
